<template>
    <d2-container>
        <!-- 搜索表头 -->
        <template slot="header">
            <el-form :inline="true" :model="searchForm" ref="searchForm" size="small" v-if="showHeader" class="searchHeader">
                <el-form-item label="所属集团" prop="pkGroup">
                    <ec-group-tree-select ref="groupTreeSelect" :visible="showHeader" v-model="searchForm.pkGroup">
                    </ec-group-tree-select>
                </el-form-item>

                <el-form-item label="下级组织" prop="pkFatherorg">
                    <ec-org-tree-select ref="orgTreeSelect" :visible="showHeader" v-model="searchForm.pkFatherorg" :pkGroup="searchForm.pkGroup">
                    </ec-org-tree-select>
                </el-form-item>

                <el-form-item label="启用状态" prop="type">
                    <ec-dict-select v-model="searchForm.enablestate" placeholder="选择启用状态" dictType="enable_state">
                    </ec-dict-select>
                </el-form-item>

                <el-form-item label="组织名称" prop="name">
                    <el-input v-model="searchForm.name" placeholder="请输入组织名称" clearable />
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="getDataTable">
                        <d2-icon name="search" />
                        查询
                    </el-button>
                    <el-button @click="handleFormReset">
                        <d2-icon name="refresh" />
                        重置
                    </el-button>
                </el-form-item>

            </el-form>
        </template>

        <!-- 表格表头 -->
        <div class="table-header">
            <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="addOrUpdate()">添加
                </el-button>
                <el-button type="primary" icon="el-icon-caret-top" size="small" v-if="!tigger"
                    @click="tableExpandTigger()">展开</el-button>
                <el-button type="primary" icon="el-icon-caret-bottom" size="small" v-else @click="tableExpandTigger()">
                    合并
                </el-button>
                <!-- <el-button type="primary" icon="el-icon-search" size="small">搜索</el-button> -->
            </div>
            <div>
                <ec-prompt-button content="刷新" icon="el-icon-refresh" @click="getDataTable()"></ec-prompt-button>
                <ec-prompt-button content="搜索" icon="el-icon-search" @click="changeHeader()"></ec-prompt-button>
                <ec-prompt-button content="导入" icon="el-icon-download"></ec-prompt-button>
                <ec-prompt-button content="导出" icon="el-icon-upload2"></ec-prompt-button>
                <ec-prompt-button content="分享" icon="el-icon-share"></ec-prompt-button>
            </div>
        </div>


        <!-- 表格表体 -->
        <ec-table :data="tabledata" ref="dataTable" row-key="pkOrg"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}" resizable v-loading="loading">
            <el-table-column prop="name" header-align="center" label="组织名称" width="220">
            </el-table-column>
            <el-table-column prop="fatherorgName" header-align="center" align="center" label="上级组织">
            </el-table-column>
            <el-table-column prop="groupName" header-align="center" align="center" label="所属集团">
            </el-table-column>
            <el-table-column prop="shortname" header-align="center" align="center" label="简称">
            </el-table-column>
            <el-table-column prop="principalName" header-align="center" align="center" label="负责人">
            </el-table-column>
            <el-table-column prop="tel" header-align="center" align="center" label="电话">
            </el-table-column>
            <el-table-column prop="enablestateDesc" header-align="center" align="center" label="启用状态">
            </el-table-column>
            <el-table-column prop="innercode" header-align="center" align="center" label="内部编码">
            </el-table-column>
            <el-table-column prop="dataoriginflagDesc" header-align="center" align="center" label="数据来源" width="180">
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="180" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" class="el-icon-edit" @click="addOrUpdate(scope.row.pkOrg)">修改
                    </el-button>
                    <el-button type="text" size="small" class="el-icon-delete" @click="deleteHandle(scope.row.pkOrg)">删除
                    </el-button>
                </template>
            </el-table-column>
        </ec-table>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataTable"></add-or-update>
    </d2-container>
</template>

<script>
    import * as orgService from '@/api/upms/org'
    import AddOrUpdate from './add-or-update'

    export default {
        name:"upms-orgs-index",
        data() {
            return {
                searchForm: {
                    enablestate: 'Y',
                    name: '',
                    pkGroup: '',
                    pkFatherorg: ''
                },
                tigger: false,
                showHeader: true,
                tabledata: [],
                loading: false,
                addOrUpdateVisible: false
            }
        },
        components: {
            AddOrUpdate
        },
        mounted() {
            this.getDataTable()
        },
        methods: {
            // 展开/合并
            tableExpandTigger() {
                this.tigger = !this.tigger
                let rows = this.tabledata
                this.dg(rows, this.tigger)
            },
            dg(rows, status) {
                let that = this
                rows.forEach((item, index) => {
                    if (item.children) {
                        that.dg(item.children, status)
                        this.$refs.dataTable.$refs.elTable.toggleRowExpansion(item, status)
                    }
                })
            },
            // 获取数据列表
            getDataTable() {
                this.loading = true
                let query = {
                    enablestate: this.searchForm.enablestate,
                    name: this.searchForm.name,
                    pkGroup: this.searchForm.pkGroup,
                    pkFatherorg: this.searchForm.pkFatherorg,
                };
                orgService.getorgtree(query).then(data => {
                    this.tabledata = data;
                    this.loading = false;
                }).catch((res) => {
                    this.loading = false;
                });
            },
            // 切换显示搜索表头
            changeHeader() {
                this.showHeader = !this.showHeader
            },
            // 重置搜索表单
            handleFormReset() {
                this.$refs.searchForm.resetFields()
                this.$refs.groupTreeSelect.resetFields()
                this.$refs.orgTreeSelect.resetFields()
            },
            // 新增 / 修改
            addOrUpdate(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            tiggerHandler(value) {
                if (value == 1) {
                    this.tigger = true;
                } else {
                    this.tigger = false;
                }
            },
            // 删除
            deleteHandle(id) {
                if (id) {
                    this.$confirm("确认删除？", "确认信息", {
                        distinguishCancelAndClose: true,
                        confirmButtonText: "删除",
                        cancelButtonText: "取消"
                    }).then(() => {
                        orgService.delorg(id).then(() => {
                            this.getDataTable();
                        });
                    });
                }
            }
        }
    }
</script>


<style>
    .header-row-style {
        background: #ccc;
    }

    .table-header {
        margin-bottom: 10px;
        overflow: auto;
        _height: 1%
    }

    .table-header>div:nth-of-type(1) {
        float: left;
    }

    .table-header>div:nth-of-type(2) {
        float: right;
    }
</style>